package com.liu.HomeWork;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 * 发送端
 */
public class HomeWork02SenderB {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(6667);
        byte[] bytes = "四大名著有哪些".getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("192.168.56.1"), 6666);
        datagramSocket.send(datagramPacket);

        byte[] buf = new byte[1024];
        DatagramPacket datagramPacket2 = new DatagramPacket(buf, buf.length);
        datagramSocket.receive(datagramPacket2);
        int length = datagramPacket2.getLength();
        String str = new String(datagramPacket2.getData(), 0, length);
        System.out.println(str);

        datagramSocket.close();
    }
}
